package my.mark.mybaibaoxiang.juc.biji07.Atomic;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

/**
 * @author wxming.
 * @description: AtomicIntegerFieldUpdater基本使用。
 */
public class AtomicIntegerFieldUpdateTest {

    //属性原子修改器。要修改的类和要修改的属性。
    static AtomicIntegerFieldUpdater aifu = AtomicIntegerFieldUpdater.newUpdater(Student.class, "old");

    public static void main(String[] args) {
        Student stu = new Student("杨过", 18);
        System.out.println(aifu.getAndIncrement(stu));
        System.out.println(aifu.get(stu));
    }

    static class Student {
        private String name;
        public volatile int old;//TODO 用volatile修饰。保证高并发下的可见性？因为此处一旦使用Atomic做修改，证明都是高并发下的场景。

        public Student(String name, int old) {
            this.name = name;
            this.old = old;
        }

        public String getName() {
            return name;
        }

        public int getOld() {
            return old;
        }
    }

}
